﻿$(function () {
    var $fristPage = $("#fristPage");
    var $previousPage = $("#previousPage");
    var $nextPage = $("#nextPage");
    var $lastPage = $("#lastPage");

    var currentPage = 0;
    var myfavoritesTotlaPage = $("#myfavoritesTotlaPage").html();
    contrlPageNumberDisplay();
    /*-------------分页---------------*/
    //控制页码是否显示
    function contrlPageNumberDisplay() {
        $('#currentPage').html(currentPage + 1);
        cancelThisFav();
        //当处于首页的时候,'首页''上一页'隐藏
        if (currentPage == 0) {
            $previousPage.hide();
        }
        else {
            $previousPage.show();
        }
        //当处于末页的时候,'末页','下一页'隐藏
        if (currentPage == (myfavoritesTotlaPage - 1)) {
            $nextPage.hide();
        }
        else {
            $nextPage.show();
        }
    }
  
    //首页
    $fristPage.click(function (e) {
        $.get("MyFavoritesByAjaxRequest?pageIndex=0", function (data) {
            currentPage = 0;
            $('#myfavoriteslist').empty();
            $('#myfavoriteslist').html(data);//替换成新的数据
            contrlPageNumberDisplay();
        });
    });

    //上一页
    $previousPage.click(function (e) {
        $.get("MyFavoritesByAjaxRequest?pageIndex=" + (currentPage - 1), function (data) {
            currentPage = currentPage - 1;
            $('#myfavoriteslist').empty();
            $('#myfavoriteslist').html(data);//替换成新的数据
            contrlPageNumberDisplay();
        });
    });

    //下一页
    $nextPage.click(function (e) {
        $.get("MyFavoritesByAjaxRequest?pageIndex=" + (currentPage + 1), function (data) {
            currentPage = currentPage + 1;
            $('#myfavoriteslist').empty();
            $('#myfavoriteslist').html(data);//替换成新的数据
            contrlPageNumberDisplay();
        });
    });

    //末页
    $lastPage.click(function (e) {
        $.get("MyFavoritesByAjaxRequest?pageIndex=" + (myfavoritesTotlaPage -1), function (data) {
            currentPage = myfavoritesTotlaPage - 1;
            $('#myfavoriteslist').empty();
            $('#myfavoriteslist').html(data);//替换成新的数据
            contrlPageNumberDisplay();
        });
    });

});

function cancelThisFav() {
    $('.cancelThisFav').on('click', function () {
        var id = $(this).data('id');
        $(this).fastConfirm({
            position: "left",
            questionText: "您确定取消该收藏吗?",
            proceedText: "确定取消",
            cancelText: "暂不取消",
            onProceed: function (trigger) {
                deleteMyfavorites(id);
            },
            onCancel: function (trigger) {
                $(trigger).fastConfirm('close');
            }
        });
    });
}

//添加到购物车
function addToCart(productId, qty) {
    var self = $(this);
    try { 
        KevinShop.Common.ShoppingCart.UpdateToCart(productId, qty);
        $('.addToBagTips' + productId).text('添加到购物车成功').fadeIn().delay(1000).fadeOut();
        //KevinShop.MessageBox.Success("添加到购物车成功！");
    } catch (e) {
        KevinShop.MessageBox.Alert(e);
    }
}

//取消收藏
function deleteMyfavorites(productId) {
    //KevinShop.MessageBox.Confirm("", "您确定取消吗？", function (isConfirm) {
    //    if (isConfirm) {
    //        $.ajax({
    //            type: "GET",
    //            url: "CancelFavority?productId=" + productId,
    //            success: function (response) {
    //                window.location.reload();
    //            },
    //            error: function (err) {
    //                alert(err.responseText);
    //            }
    //        });
    //    }
    //});
    $.ajax({
        type: "GET",
        url: "CancelFavority?productId=" + productId,
        success: function (response) {
            window.location.reload();
        },
        error: function (err) {
            alert(err.responseText);
        }
    });
};